package com.carleasoft.mps.cms.business.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.user.bean.UserSkill;
import com.carleasoft.mps.cms.business.user.bean.UserSkillType;
import com.carleasoft.mps.cms.business.user.dao.UserSkillTypeMapper;
import com.carleasoft.mps.cms.business.user.service.UserSkillService;
import com.carleasoft.mps.cms.business.user.service.UserSkillTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zang
 * @since 2023-05-25
 */
@Service
public class UserSkillTypeServiceImpl extends ServiceImpl<UserSkillTypeMapper, UserSkillType> implements UserSkillTypeService {
    @Autowired
    private UserSkillService userSkillService;
    @Autowired
    private UserSkillTypeService userSkillTypeService;
    @Override
    public void updateSkillType(UserSkillType userSkillType) {
        if(StringUtils.isNotBlank(userSkillType.getUserSkillType())){
            // 更新
            this.update(new UpdateWrapper<UserSkillType>()
                            .eq("USER_SKILL_TYPE",userSkillType.getUserSkillType())
                    .set("USER_SKILL_TYPE_NAME",userSkillType.getUserSkillTypeName())
                    .set("USER_SKILL_PARENT_TYPE",userSkillType.getUserSkillParentType()));
        } else {
            // 保存
            userSkillType.setUserSkillType(UUID.randomUUID().toString());
            if(StringUtils.isNotBlank(userSkillType.getUserSkillParentType())){
                userSkillType.setUserSkillParentType(userSkillType.getUserSkillParentType());
            }
            this.save(userSkillType);
        }
    }

    @Override
    public boolean deleteSkillType(UserSkillType userSkillType) {
        UserSkill userSkill = new UserSkill();
        userSkill.setUserSkillType(userSkillType.getUserSkillType());
        List<UserSkill> list = userSkillService.getUserSkillTypeList(userSkill);
        if(list.size()>0){
            return false;
        } else {
            // 删除数据
            this.remove(new QueryWrapper<UserSkillType>().eq("USER_SKILL_TYPE",userSkillType.getUserSkillType()));
            return  true;
        }
    }

    @Override
    public List<UserSkillType> getAllWrapper() {
        LambdaQueryWrapper<UserSkillType> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(UserSkillType::getUserSkillParentType);
        //查询根级
        List<UserSkillType> regions = baseMapper.selectList(wrapper);

        List<UserSkillType> list = regions.stream().map(p -> {
            UserSkillType obj = new UserSkillType();
            BeanUtil.copyProperties(p, obj);
            return obj;
        }).collect(Collectors.toList());

        list.forEach(this::getChildren);
        return list;
    }

    private void getChildren(UserSkillType item){
        LambdaQueryWrapper<UserSkillType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserSkillType::getUserSkillParentType, item.getUserSkillType());
        //根据parentId查询
        List<UserSkillType> list = baseMapper.selectList(wrapper);

        List<UserSkillType> voList = list.stream().map(p -> {
            UserSkillType vo = new UserSkillType();
            BeanUtil.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());
        //写入到children
        item.setChildren(voList);

        //如果children不为空，继续往下找
        if (!CollectionUtils.isEmpty(voList)) {
            voList.forEach(this::getChildren);
        }
    }
}
